package com.qms.qep.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.core.page.TableDataInfo;
import com.qms.common.enums.BusinessType;
import com.qms.qep.domain.QepSignatory;
import com.qms.qep.service.IQepSignatoryService;

/**
 * 签名人管理Controller
 * 
 * @author qms
 * @date 2023-05-20
 */
@RestController
@RequestMapping("/qep/certificate/signatory")
public class QepSignatoryController extends BaseController
{
    @Autowired
    private IQepSignatoryService qepSignatoryService;

    /**
     * 查询签名人列表
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:list')")
    @GetMapping("/list")
    public TableDataInfo list(QepSignatory qepSignatory)
    {
        startPage();
        List<QepSignatory> list = qepSignatoryService.selectQepSignatoryList(qepSignatory);
        return getDataTable(list);
    }

    /**
     * 获取签名人详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(qepSignatoryService.selectQepSignatoryById(id));
    }

    /**
     * 新增签名人
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:add')")
    @Log(title = "签名人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QepSignatory qepSignatory)
    {
        return toAjax(qepSignatoryService.insertQepSignatory(qepSignatory));
    }

    /**
     * 修改签名人
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:edit')")
    @Log(title = "签名人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QepSignatory qepSignatory)
    {
        return toAjax(qepSignatoryService.updateQepSignatory(qepSignatory));
    }

    /**
     * 删除签名人
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:remove')")
    @Log(title = "签名人管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qepSignatoryService.deleteQepSignatoryByIds(ids));
    }
    
    /**
     * 保存签名人电子签名
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:edit')")
    @Log(title = "签名人管理", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    public AjaxResult saveSignatory(@RequestBody QepSignatory signatory)
    {
        QepSignatory existingSignatory = qepSignatoryService.selectQepSignatoryById(signatory.getId());
        
        // 更新签名图片和签名类型
        existingSignatory.setImageUrl(signatory.getImageUrl());
        existingSignatory.setSignatureType(signatory.getSignatureType());
        
        return toAjax(qepSignatoryService.updateQepSignatory(existingSignatory));
    }
    
    /**
     * 添加电子签名
     */
    @PreAuthorize("@ss.hasPermi('qep:certificate:signatory:edit')")
    @Log(title = "签名人管理", businessType = BusinessType.UPDATE)
    @PostMapping("/signature/add")
    public AjaxResult addDigitalSignature(@RequestBody Map<String, Object> params)
    {
        Long signatoryId = Long.valueOf(params.get("signatoryId").toString());
        String signatureData = params.get("signatureData").toString();
        
        QepSignatory signatory = qepSignatoryService.selectQepSignatoryById(signatoryId);
        signatory.setImageUrl(signatureData);
        signatory.setSignatureType("digital");
        
        return toAjax(qepSignatoryService.updateQepSignatory(signatory));
    }
} 